import { Fields, TableAction } from "@/types/public";
import { Button, Popconfirm, Space, InputNumber } from "antd";
import React from "react";
import { EditOutlined, DeleteOutlined } from "@ant-design/icons";

// 设备节点数据结构定义
export interface DataListsField {
  /** 唯一标识 */
  id: number;

  /** 所属模板ID(外键关联template表) */
  templateId: number;

  /** 从设备节点名称 */
  nodeName: string;

  /** 排序序号 */
  sortOrder: number;

  /** 数据类型 */
  dataType: string;

  /** 从设备地址 */
  slaveAddress: string;

  /** 寄存器/偏移量 */
  registerOffset?: string;

  /** 存储策略 */
  storagePolicy?: string;

  /** 数据精度 */
  dataPrecision?: string;

  /** 平台到设备公式处理 */
  formulaProcessing1?: string;

  /** 设备到平台公式处理 */
  formulaProcessing2?: string;

  /** 属性名称 */
  attributeName?: string;

  /** 创建时间 */
  createdAt?: string | Date;

  /** 更新时间 */
  updatedAt?: string | Date;
}

export const createDataListsFieldField = (
  tableAction: TableAction<DataListsField> = {}
): Fields[] => {
  return [
    {
      valueType: "number",
      inputType: "input",
      key: "id",
      title: "ID",
      disabled: true,
      dataIndex: "id",
      width: 80,
      isEdit: false,
      visible: true,
      rules: [{ required: false }],
    },
    {
      valueType: "number",
      inputType: "input-number",
      title: "模板ID",
      dataIndex: "templateId",
      key: "templateId",
      placeholder: "请输入所属模板ID",
      rules: [
        { required: true, message: "模板ID不能为空" },
        { type: "number", min: 1, message: "模板ID必须大于0" },
      ],
    },
    {
      valueType: "string",
      inputType: "input",
      title: "从设备节点名称",
      dataIndex: "nodeName",
      key: "nodeName",
      placeholder: "请输入从设备节点名称",
      rules: [
        { required: true, message: "从设备节点名称不能为空" },
        { max: 255, message: "名称长度不能超过255字符" },
      ],
    },
    {
      valueType: "number",
      inputType: "input-number",
      title: "排序序号",
      dataIndex: "sortOrder",
      key: "sortOrder",
      placeholder: "请输入排序序号",
      rules: [
        { required: true, message: "排序序号不能为空" },
        { type: "number", min: 0, message: "排序序号必须大于等于0" },
      ],
    },
    {
      valueType: "string",
      inputType: "select",
      title: "数据类型",
      dataIndex: "dataType",
      key: "dataType",
      placeholder: "请选择数据类型",
      options: [
        { value: "int", label: "整数" },
        { value: "float", label: "浮点数" },
        { value: "string", label: "字符串" },
        { value: "boolean", label: "布尔值" },
        { value: "timestamp", label: "时间戳" },
      ],
      rules: [{ required: true, message: "请选择数据类型" }],
    },
    {
      valueType: "string",
      inputType: "input",
      title: "从设备地址",
      dataIndex: "slaveAddress",
      key: "slaveAddress",
      placeholder: "请输入从设备地址",
      rules: [
        { required: true, message: "从设备地址不能为空" },
        { max: 255, message: "地址长度不能超过255字符" },
      ],
    },
    {
      valueType: "string",
      inputType: "input",
      title: "寄存器/偏移量",
      dataIndex: "registerOffset",
      key: "registerOffset",
      placeholder: "请输入寄存器/偏移量",
      rules: [{ required: false }],
    },
    {
      valueType: "string",
      inputType: "select",
      title: "存储策略",
      dataIndex: "storagePolicy",
      key: "storagePolicy",
      placeholder: "请选择存储策略",
      options: [
        { value: "realtime", label: "实时存储" },
        { value: "timed", label: "定时存储" },
        { value: "batch", label: "批量存储" },
      ],
      rules: [{ required: false }],
    },
    {
      valueType: "string",
      inputType: "select",
      title: "数据精度",
      dataIndex: "dataPrecision",
      key: "dataPrecision",
      placeholder: "请选择数据精度",
      options: [
        { value: "0", label: "整数" },
        { value: "0.1", label: "保留1位小数" },
        { value: "0.01", label: "保留2位小数" },
        { value: "0.001", label: "保留3位小数" },
      ],
      rules: [{ required: false }],
    },
    {
      valueType: "string",
      inputType: "textarea",
      title: "平台到设备公式",
      dataIndex: "formulaProcessing1",
      key: "formulaProcessing1",
      placeholder: "请输入平台到设备的公式处理",
      autoSize: { minRows: 2, maxRows: 4 },
      rules: [{ required: false }],
    },
    {
      valueType: "string",
      inputType: "textarea",
      title: "设备到平台公式",
      dataIndex: "formulaProcessing2",
      key: "formulaProcessing2",
      placeholder: "请输入设备到平台的公式处理",
      autoSize: { minRows: 2, maxRows: 4 },
      rules: [{ required: false }],
    },
    {
      valueType: "string",
      inputType: "select",
      title: "属性名称",
      dataIndex: "attributeName",
      key: "attributeName",
      placeholder: "请选择属性名称",
      options: [
        { value: "temperature", label: "温度" },
        { value: "humidity", label: "湿度" },
        { value: "pressure", label: "压力" },
        { value: "voltage", label: "电压" },
        { value: "current", label: "电流" },
        { value: "power", label: "功率" },
      ],
      rules: [{ required: false }],
    },
    {
      valueType: "string",
      inputType: "date-picker",
      title: "创建时间",
      dataIndex: "createdAt",
      key: "createdAt",
      disabled: true,
      visible: false,
      rules: [{ required: false }],
    },
    {
      valueType: "string",
      inputType: "date-picker",
      title: "更新时间",
      dataIndex: "updatedAt",
      key: "updatedAt",
      disabled: true,
      visible: false,
      rules: [{ required: false }],
    },
    {
      title: "操作",
      key: "action",
      fixed: "right",
      width: 200,
      render: (_: any, record: DataListsField) => (
        <Space size="middle">
          <Button
            type="link"
            size="small"
            icon={<EditOutlined />}
            onClick={() => tableAction.edit?.(record)}
          >
            编辑
          </Button>
          <Popconfirm
            title="确定删除该节点？"
            description="删除节点将无法恢复"
            onConfirm={() => tableAction.delete?.([record.id])}
          >
            <Button type="link" danger icon={<DeleteOutlined />}>
              删除
            </Button>
          </Popconfirm>
        </Space>
      ),
    },
  ];
};
